<template>
  <div class="home">
    <div class="banner">
      <img src="../assets/avatar.jpg" alt />
    </div>
    <h3>爱时尚精品屋</h3>
    <h6>公告：欢迎光临，流行爆款库存有限请尽快下单</h6>
    <div class="nav">
      <div :class='{"nav_item":true,active:oIndex===index}' v-for="(item,index) in navlist"
        :key="index"
        @click="saveIndex(index)"
      >
        <div>{{item}}</div>
      </div>
    </div>
    <div class="list">
      <div class="item" v-for="(item,index) in list" :key="index">
        <div class="img">
          <img :src="item.pic" alt />
        </div>
        <div class="text">
          <div class="title">{{item.title}}</div>
          <div>已售：{{item.ys}}件</div>
          <div>
            单价：
            <span>￥{{item.dj}}</span>
          </div>
        </div>
        <div class="ytjgwc" @click="add(item)" v-if="item.count>0">已添加</div>
        <div class="jrgwc" @click="add(item)" v-else>加入购物车</div>
      </div>
      <router-link to="/cart" class="gwc"><img src="../assets/cart.png" alt="">
      <span>{{gwclist.length}}</span>
      </router-link>
     
    </div>
  </div>
</template>


<script>
import { mapState, mapGetters } from "vuex";
import axios from "axios";
export default {
  data() {
    return {
      oIndex: 0,
      list: [],
      navlist: ["价格↑", "价格↓", "销量↑", "销量↓"]
    };
  },
  mounted() {
    axios.get("/list").then(res => {
      this.list = res.data.list;
    });
  },
  computed:{
        ...mapState(["gwclist"])
    },
   
  methods: {
    saveIndex(index) {
      this.oIndex = index;
      if (index === 0) {
        this.list.sort((a, b) => {
          return a.dj - b.dj;
        });
      } else if (index === 1) {
        this.list.sort((a, b) => {
          return b.dj - a.dj;
        });
      } else if (index === 2) {
        this.list.sort((a, b) => {
          return a.ys - b.ys;
        });
      } else {
        this.list.sort((a, b) => {
          return b.ys - a.ys;
        });
      }
    },
  add(item){
    this.$store.commit('add', item);
  }
  }
};
</script>
<style lang="scss">
.home {
  width: 100%;
  height: 667px;
  display: flex;
  flex-direction: column;
  .banner {
    background-image: url("../assets/banner-2.png");
    background-size: cover;
    background-position-y: center;
    width: 100%;
    height: 160px;
    position: relative;
    img {
      width: 80px;
      position: absolute;
      bottom: -40px;
      left: 50%;
      margin-left: -40px;
      border-radius: 50%;
    }
  }
  h3 {
    width: 100%;
    height: 50px;
    text-align: center;
    line-height: 50px;
    padding-top: 40px;
  }
  h6 {
    text-align: center;
  }
  .nav {
    width: 100%;
    height: 40px;
    line-height: 40px;
    display: flex;
    text-align: center;
    border-bottom: 1px solid #ccc;
    div {
      flex: 1;
    }
    .active {
      color: red;
      border-bottom: 2px solid red;
    }
  }
  .list {
    width: 100%;
    height: 427px;
    overflow: auto;
    .gwc {
      width: 60px;
      height: 60px;
      background: skyblue;
      color: #fff;
      position: fixed;
      right: 30px;
      bottom: 50px;
      line-height: 60px;
      text-align: center;
      border-radius: 50%;
      img{
        width: 30px;
      height: 30px;
      margin-top: 15px;
      }
      span{
        position: absolute;
        top: 0;
        width: 20px;
        height: 20px;
        background: red;
        color: #fff;
        text-align: center;
        line-height: 20px;
        border-radius: 50%;
        right: 0;
        top: 0;
      }
    }
    .item {
      display: flex;
      padding: 20px;
      position: relative;
      border-bottom: 1px solid #eee;
      .img {
        width: 60px;
        height: 60px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .text {
        flex: 1;
        padding: 0 20px;
        span {
          color: red;
        }
      }
      .jrgwc,.ytjgwc{
        position: absolute;
        bottom: 20px;
        right: 10px;
        background: red;
        color: aliceblue;
        padding: 2px 5px;
      }
      .ytjgwc{
        background: #ccc;
        color:#000;
      }
    }
  }
}
</style>